package cn.itcast.web;

import cn.itcast.pojo.evaluate;
import cn.itcast.pojo.order;
import cn.itcast.service.impl.evaluateServiceImpl;
import cn.itcast.util.OutputStringController;
import cn.web.web_module.util.IconPath;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

@Slf4j
@RestController
@RequestMapping(value="/wxshop/user",produces="text/html;charset=UTF-8")
public class evaluateController {
//    @Autowired
//    private DataSource dataSource;
    @Autowired
    private StringRedisTemplate template;
    @Resource
    evaluateServiceImpl evaluateService;
    @Resource
    OutputStringController osc;



    /**
     *
     * @Description:
     * 生成订单
     * @auther: 欧阳辉
     * @date: 8:12 2020/3/17
     * @param: [request]
     * @return: java.lang.String
     *
     */
    @PostMapping("uploadEvaluate")
    public String uploadEvaluate(HttpServletRequest request) throws IllegalStateException, IOException {
        //返回结果
        String results="";

        //获取订单编号
        String id = request.getParameter("evaluateId");


        //将当前上下文初始化给  CommonsMutipartResolver （多部分解析器）
        CommonsMultipartResolver multipartResolver=new CommonsMultipartResolver(
                request.getSession().getServletContext());
        //检查form中是否有enctype="multipart/form-data"
        if(multipartResolver.isMultipart(request))
        {
            //将request变成多部分request
            MultipartHttpServletRequest multiRequest=(MultipartHttpServletRequest)request;
            //获取multiRequest 中所有的文件名
            Iterator iter=multiRequest.getFileNames();

            while(iter.hasNext())
            {
                //一次遍历所有文件
                MultipartFile file=multiRequest.getFile(iter.next().toString());
                if(file!=null)
                {
                    SimpleDateFormat sdf=new SimpleDateFormat("yyyyMMddHHmmss");
                    String newDate=sdf.format(new Date());
                    //服务器存储图片地址
                    //服务器地址：“/usr/tomcat/images/wxshop/evaluate/”
                    //String filePath= "D:/Desktop/icon/evaluate/"+newDate+"/"+file.getOriginalFilename();
//                    String filePath="/usr/tomcat/images/dhn/icon/evaluate/"+newDate+"/"+file.getOriginalFilename(); //松注释
//                    File files=new File("/usr/tomcat/images/dhn/icon/evaluate/"+newDate);   //松注释
                    String filePath= IconPath.ICONPATH +"icon/evaluate/"+newDate+"/"+file.getOriginalFilename();
                    File files=new File(IconPath.ICONPATH +"icon/evaluate/"+newDate);
                    //File files=new File("D:/Desktop/icon/evaluate/"+newDate);
                    if(!files.exists()){//如果文件夹不存在
                        files.mkdir();//创建文件夹
                    }
                    //数据库存放访问地址
                    String path="https://www.dhnep.cn:8181/dhn/icon/evaluate/"+newDate+"/"+file.getOriginalFilename();
                    boolean isadd = evaluateService.uploadEvaluate(id,path);
                    if(isadd) {
                        //上传图片至服务器
                        file.transferTo(new File(filePath));
                        results = osc.success("yes");
                        log.debug("上传成功");
                    }else {
                        results = osc.failure("no");
                        log.debug("上传失败");
                    }

                }

            }

        }


        return results;
    }


    /**
     *
     * @Description:
     * 生成评价
     * @auther: 欧阳辉
     * @date: 8:12 2020/3/17
     * @param: [request]
     * @return: java.lang.String
     *
     */
    @PostMapping("addEvaluate")
    public String addEvaluate(@RequestBody Map<String,String> map) {
        //返回结果
        String results="";
        evaluate evaluate = new evaluate();
        log.debug("添加评价开始");
        evaluate.setEvaluateContent(map.get("evaluateContent"));
        evaluate.setEvaluateGrade(Integer.parseInt(map.get("evaluateGrade")));
        evaluate.setOpenId(map.get("openId"));
//        evaluate.setProductId(Integer.parseInt(request.getParameter("productId")));
        evaluate.setOrderId(Integer.parseInt(map.get("orderId")));


        Integer id = evaluateService.addEvaluate(evaluate);
        if(true) {
            log.debug(id.toString());
            results = osc.resultSuccess("yes",id);
            log.debug("上传成功");
        }else {
            results = osc.failure("no");
            log.debug("上传失败");
        }







        return results;
    }


    /**
     *功能：查找订单评价
     * @author 欧阳辉
     * @date 2019/6/18
      * @param map
     * @return java.lang.String
     */
    @PostMapping("findOrderEvaluate")
    public String findOrderEvaluate(@RequestBody Map<String,String> map) {
        //返回结果
        String results="";
        //订单Id
        Integer orderId = Integer.parseInt(map.get("orderId"));
        String openId = (map.get("openId"));

        evaluate evaluate = evaluateService.findOrderEvaluate(orderId,openId);


        if(evaluate==null){
            log.debug("订单商品未评价");
            results = osc.resultFailure("no","");
        }else {
            log.debug("获取订单评价成功");
            results = osc.resultSuccess("yes",evaluate);
        }
        return results;
    }

}
